<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="用户管理" name="first">
            <el-row style="height:60px">
              <el-button type="primary" icon="el-icon-plus" size="small" @click="addRoles">新增角色</el-button>
            </el-row>
            <!-- 表格组件 -->
            <el-table border style="width: 100%" :data="roleList">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column label="角色名" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="setRoles(scope.row)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRoles(scope.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRoles(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页区域 -->
            <el-pagination
              :current-page="query.page"
              :page-sizes="[10, 15, 20, 25]"
              :page-size="query.size"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px" :model="CompanyInfo">
              <el-form-item label="公司名称">
                <el-input v-model="CompanyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="CompanyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="CompanyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="CompanyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
        <!-- 新增角色弹框 -->
        <el-dialog
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          title="新增角色"
          :visible.sync="showDialog"
          width="50%"
          @close="onCancel"
        >
          <el-form ref="formDate" :model="formDate" :rules="rules" label-width="100px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="formDate.name" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="formDate.description" />
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="onCancel">取 消</el-button>
            <el-button type="primary" @click="onSubmit">确 定</el-button>
          </span>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getRoles, getCompanyInfo, addRole, getRoleDetail, updateRoleInfo, delRole } from '@/api/setting'
import { mapGetters } from 'vuex'
export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'first',
      query: {
        page: 1,
        size: 10
      },
      roleList: [],
      total: 0,
      CompanyInfo: {},
      formDate: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      },
      showDialog: false,
      isEdit: false
    }
  },
  computed: {
    ...mapGetters([
      'companyId'
    ])
  },
  created() {
    this.getRoles()
    this.getCompanyInfo()
  },
  methods: {
    setRoles() {},

    async delRoles(id) {
      const confirmRes = await this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes === 'cancel') return this.$message('已取消了删除')
      const res = await delRole(id)
      if (this.roleList.length === 1) this.query.page--
      if (this.query.page < 1) this.query.page = 1
      this.$message.success(res.message)
      this.getRoles()
    },

    async editRoles(id) {
      this.showDialog = true
      this.isEdit = true
      const res = await getRoleDetail(id)
      console.log(res)
      this.formDate = res.data
    },
    // 新增按钮
    addRoles() {
      this.showDialog = true
      this.isEdit = false
    },
    // 新增或编辑
    onSubmit() {
      this.$refs.formDate.validate(async valid => {
        if (!valid) return
        if (this.isEdit) {
          const res = await updateRoleInfo(this.formDate)
          this.$message.success(res.message)
        } else {
          const res = await addRole(this.formDate)
          // console.log(res)
          this.$message.success(res.message)
        }
        this.getRoles()
        this.showDialog = false
      })
    },
    // 取消
    onCancel() {
      this.$refs.formDate.resetFields()
      this.showDialog = false
    },

    // 获取所有角色列表
    async getRoles() {
      const res = await getRoles(this.query)
      // console.log(res)
      this.roleList = res.data.rows
      this.total = res.data.total
    },
    // 获取公司信息
    async getCompanyInfo() {
      const res = await getCompanyInfo(this.companyId)
      this.CompanyInfo = res.data
    },
    // 分页条数
    handleSizeChange(newSize) {
      this.query.size = newSize
      this.getRoles()
    },
    // 分页页码
    handleCurrentChange(newPage) {
      this.query.page = newPage
      this.getRoles()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card.is-always-shadow {
    padding: 10px 30px;
}
.el-pagination {
    margin-top: 15px;
    text-align: center;
}
::v-deep .el-dialog__body {
    padding: 30px 30px;
}
</style>
